package q49_groupAnagrams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution_1 {
    public static void main(String[] args) {
        String[] strs = {"a"};
        Solution_1 s = new Solution_1();
        List<List<String>> a = s.groupAnagrams(strs);
        System.out.println(a);
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        List<String> bin = new ArrayList<>();
        Map<Map<Character,Integer>,List<String>> test = new HashMap<>();
        // List<Map<Character,Integer>> maps = new ArrayList<>();
        // Map<Character, Integer> map = new HashMap<>();
        for (String str : strs) {
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < str.length(); i++) {
                if (map.containsKey(str.charAt(i))) {
                    map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
                }else {
                    map.put(str.charAt(i), 1);
                }
            }

            if (test.containsKey(map)) {
                bin = test.get(map);
                // System.out.println(bin);
            }else {
                bin = new ArrayList<>();
            }
            bin.add(str);
            test.put(map,bin);
            // maps.add(map);
        }
//        Set<Map<Character, Integer>> liStrings = test.keySet();//获取hashmap中的所有的key的集合
//        for (Map<Character, Integer> string : liStrings) {
//            // System.out.println(string+"==="+test.get(string));
//            res.add(test.get(string));
//        }
        // 直接食用Arraylist的构造函数可以将values转换为List
        return new ArrayList<List<String>>(test.values());
        // return res;
    }
}
